package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.douluodalu.CrossDouLuoDaLuContext;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;

@Cmd(code = Protocol.C_DOU_LUO_DA_LU_RED_DOT, desc = "红点")
public class CrossDouLuoDaLuRedDotCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        DouLuoDaLuProto.DouLuoDaLuCommonCrossReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuCommonCrossReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        CrossDouLuoDaLuContext context = CrossDouLuoDaLuContext.initContext(activityId, packet.getPlayerId()).loadTeamData();
        if(context.getRet() == 0){
            context.getActivity().checkUserRedHot(context.getUserData(), null);

            int second = DateHelper.getCurrentSecond();
            //队长入队红点
            if(context.getTeamData().getCaptainId() == packet.getPlayerId()){
                boolean newApply = false;
                for (Map.Entry<Long, Long> entry : context.getTeamData().getApplyList().entrySet()) {
                    if(entry.getValue() > second){
                        newApply = true;
                        break;
                    }
                }
                if(newApply){
                    DouLuoDaLuProto.DouLuoDaLuRedDotSyncMsg.Builder redDot = DouLuoDaLuProto.DouLuoDaLuRedDotSyncMsg.newBuilder();
                    redDot.setNewApply(1);
                    MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_DOU_LUO_DA_LU_RED_DOT_SYNC, redDot));
                }
            }
        }
    }

    @Override
    public void sendData() {

    }
}
